package com.zik.rader_and_video.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zik.rader_and_video.controller.WxController;
import com.zik.rader_and_video.po.Errors;
import com.zik.rader_and_video.po.MqttRespData;
import com.zik.rader_and_video.server.WebSocketServer;
import com.zik.rader_and_video.service.IErrorsService;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author zik
 * @description
 * @time 2023/6/15 16:44
 **/
@Slf4j
@Component
public class SleepAnalysisUtils {
    @Resource(name = "IErrorsService")
    private IErrorsService errorsService;

    @Autowired
    private WxController iWxController;

    @Resource(name = "WebSocketServer")
    private WebSocketServer webSocketServer;

    private static WebSocketServer webSocketServerUtil;
    private static IErrorsService iErrorsService;

    private static WxController wxController;

    @PostConstruct
    public void init() {
        webSocketServerUtil = webSocketServer;
        iErrorsService = errorsService;
        wxController = iWxController;
    }



    private static final List<String> USEFULDATA =
            Arrays.asList(
                    "movementSigns", "breathInform", "breathInform", "breathIntensity", "breathConfidence",
                    "heartRateInform", "heartRateValue", "heartRateIntensity",
                    "sleepStatus", "sleepComprehensiveStatus", "sleepQualityAnalysis","someoneExists",
                    "humanDistance","fallStatus","fallPosition","online");

    /**
     * @author zik
     * @Param mqttMessage
     * @Return MqttRespData
     * @Time 2023/6/15 17:49
     * @description
     **/
    public static MqttRespData getParam(MqttMessage message) {
        JSONObject jsonObject = JSONObject.parseObject(message.toString());
        return JSON.toJavaObject(jsonObject, MqttRespData.class);
    }

    /**
     * @author zik
     * @Param resdata
     * @Return Boolean
     * @Time 2023/6/15 17:20
     * @description 判断是否为非主动上报数据
     **/
    public static Boolean isRespData(MqttRespData respData) {
        return respData.getRes() == null;
    }

    /**
     * 是否为非屏蔽字段
     */
    public static Boolean isUseful(MqttMessage message) {
        JSONObject jsonObject = JSONObject.parseObject(message.toString());
        JSONObject param = jsonObject.getJSONObject("params");
        for (String usefulData : USEFULDATA) {
            if (param.containsKey(usefulData)) {
                return true;
            }
        }
        log.info("忽略...");
        return false;
    }

    /**
     * @author zik
     * @Param
     * @Return null
     * @Time 2023/6/15 17:50
     * @description 根据上报的不同数据进行分流
     **/
    public static void processMqttData(MqttRespData mqttRespData) {
        Map<String, String> params = mqttRespData.getParams();
        log.info(params.toString());
        for (String usefulData : USEFULDATA) {
            if (params.containsKey(usefulData)) {
                log.info("正在处理{} --> 值为 ->{}",usefulData,params.get(usefulData));
                if ("fallStatus".equals(usefulData)&& "1".equals(params.get(usefulData))){
                    Errors errors = new Errors();
                    errors.setErrorTag(usefulData);
                    errors.setErrorData("发生跌倒");
                    errors.setErrorValue(1);
                    iErrorsService.save(errors);
                    wxController.wxPush();
                }
                processData(params,usefulData);
            }
        }
    }

    /**
     * @author zik
     * @Time 2023/6/15 18:03
     * @description 针对不同信息做不同处理
     **/
    public static void processData(Map params, String useful) {
        switch (useful) {
            // 运动幅度
            case "movementSigns":
                log.info("正在处理 ->{}<- 信息","movementSigns");
                webSocketServerUtil.sendAll(params.toString());
                System.out.println("发送movementSigns完成");
                break;
            // 呼吸信息数值
            case "breathInform":
                log.info("正在处理 ->{}<- 信息","breathInform");
                webSocketServerUtil.sendAll(params.toString());
                System.out.println("发送breathInform完成");
                break;
            // 呼吸强度
            case  "breathIntensity":
                log.info("正在处理 ->{}<- 信息","breathIntensity");
                webSocketServerUtil.sendAll(params.toString());
                System.out.println("发送breathIntensity完成");
                break;
            // 呼吸置信度
            case  "breathConfidence":
                log.info("正在处理 ->{}<- 信息","breathConfidence");
                webSocketServerUtil.sendAll(params.toString());
                System.out.println("发送breathConfidence完成");
                break;
            // 心率信息
            case  "heartRateInform":
                log.info("正在处理 ->{}<- 信息","heartRateInform");
                webSocketServerUtil.sendAll(params.toString());
                System.out.println("发送heartRateInform完成");
                break;
            // 心率数值
            case  "heartRateValue":
                log.info("正在处理 ->{}<- 信息","heartRateValue");
                webSocketServerUtil.sendAll(params.toString());
                System.out.println("发送heartRateValue完成");
                break;
            // 心率强度
            case  "heartRateIntensity":
                log.info("正在处理 ->{}<- 信息","heartRateIntensity");
                webSocketServerUtil.sendAll(params.toString());
                System.out.println("发送heartRateIntensity完成");
                break;
            // 睡眠状态
            case  "sleepStatus":
                log.info("正在处理 ->{}<- 信息","sleepStatus");
                webSocketServerUtil.sendAll(params.toString());
                System.out.println("发送sleepStatus完成");
                break;
            // 睡眠综合状态
            case  "sleepComprehensiveStatus":
                log.info("正在处理 ->{}<- 信息","sleepComprehensiveStatus");
                webSocketServerUtil.sendAll(params.toString());
                System.out.println("发送sleepComprehensiveStatus完成");
                break;
            // 睡眠质量分析
            case "sleepQualityAnalysis":
                log.info("正在处理 ->{}<- 信息","sleepQualityAnalysis");
                webSocketServerUtil.sendAll(params.toString());
                System.out.println("发送sleepQualityAnalysis完成");
                break;
            case "someoneExists":
                log.info("正在处理 ->{}<- 信息","someoneExists");
                webSocketServerUtil.sendAll(params.toString());
                System.out.println("发送someoneExists完成");
                break;
            case "humanDistance":
                log.info("正在处理 ->{}<- 信息","humanDistance");
                webSocketServerUtil.sendAll(params.toString());
                System.out.println("发送humanDistance完成");
                break;
            case "fallStatus":
                log.info("正在处理 ->{}<- 信息","fallStatus");
                webSocketServerUtil.sendAll(params.toString());
                System.out.println("发送fallStatus完成");
                break;
            case "fallPosition":
                log.info("正在处理 ->{}<- 信息","fallPosition");
                webSocketServerUtil.sendAll(params.toString());
                System.out.println("发送fallPosition完成");
                break;
            default:
                break;
        }
    }


}
